﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrazyWorship1.PropertyHandler;
using System.Globalization;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using System.Windows;
using CrazyWorship1.Assets;
using System.Windows.Media;

namespace CrazyWorship1.Controllers
{
   public class BackgroundTextureMovementController:ControllerBase
    {
       private String _AssetName;
       private double _XSpeed, _YSpeed;
       [DataAttribute("XSpeed", typeof(DoublePropertyHandler),"BackgroundTextureMovementControllerDefaultXSpeed")]
       public string XSpeed
       {
           get
           {
               return _XSpeed.ToString(CultureInfo.InvariantCulture);
           }
           set
           {
               _XSpeed = Convert.ToDouble(value, CultureInfo.InvariantCulture);
           }
       }
       [DataAttribute("YSpeed", typeof(DoublePropertyHandler), "BackgroundTextureMovementControllerDefaultYSpeed")]
       public string YSpeed
       {
           get
           {
               return _YSpeed.ToString(CultureInfo.InvariantCulture);
           }
           set
           {
               _YSpeed = Convert.ToDouble(value, CultureInfo.InvariantCulture);
           }
       }
       [DataAttribute("AssetName", typeof(StringPropertyHandler), "BackgroundTextureMovementControllerDefaultAssetName")]
       public string AssetName
       {
           get
           {
               return _AssetName.ToString();// ToString(CultureInfo.InvariantCulture);
           }
           set
           {
               _AssetName= value;
           }
       }
      
       DoubleAnimation px,py;
   //    Storyboard dx,dy;
       public override void Go()
       {
         
           
           
           Duration d = new Duration(new TimeSpan(0, 0, 0, 10, 0));

           px = new DoubleAnimation();
           px.To = _XSpeed;
           px.Duration = d;

           py = new DoubleAnimation();
           py.To = _YSpeed;
           py.Duration = d;
         
         
           ass= ParentCreator.GetAssetByName(_AssetName) as TiledBackgroundAsset;
           if (ass != null)
           {
           
               ass.Brush.RelativeTransform = new TranslateTransform();
              
               py.Completed += new EventHandler(py_Completed);
               px.Completed += new EventHandler(px_Completed);
               ass.Brush.RelativeTransform.BeginAnimation(TranslateTransform.YProperty, py);
               ass.Brush.RelativeTransform.BeginAnimation(TranslateTransform.XProperty, px);

           }
         


         //  dx.Children.Add(px);
         //  dy.Children.Add(py);

         //  dx.Completed += new EventHandler(dx_Completed);
         //  dx.Begin(CurrentState.Viewers[0]);
         //  dy.Completed += new EventHandler(dy_Completed);
        //   dy.Begin(CurrentState.Viewers[0]);
       }
       TiledBackgroundAsset ass;
       void px_Completed(object sender, EventArgs e)
       {
           px.To += _XSpeed;
           ass.Brush.RelativeTransform.BeginAnimation(TranslateTransform.XProperty, px);

       }

       void py_Completed(object sender, EventArgs e)
       {
           py.To += _YSpeed;
           ass.Brush.RelativeTransform.BeginAnimation(TranslateTransform.YProperty, py);
             
       }

       //void dx_Completed(object sender, EventArgs e)
       //{
       //    px.To = _XSpeed;
       //    dx.Begin(CurrentState.Viewers[0]);
       //}

       //void dy_Completed(object sender, EventArgs e)
       //{
       //    px.To = _YSpeed;
       //    dy.Begin(CurrentState.Viewers[0]);
       //}
   
      
    }
}
